﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotSpatial.Controls;


namespace HAGISV2
{
    [Serializable]
    public class LayerCollection : IList<Layer>
    {
        public LayerCollection()
        {

            _innerList = new List<Layer>();
        }


        [NonSerialized]
        GisManager _defaultManager;

        /// <summary>
        /// 管理器的句柄
        /// </summary>
        public GisManager DefaultManager
        {
            get { return _defaultManager; }
            set
            {
                this._defaultManager = value;


                //重置所有元素

                foreach (Layer item in this._innerList)
                {
                    item.DefaultManager = this._defaultManager;

                }



            }

        }




        List<Layer> _innerList;


        #region 自定义的接口部分

        
        /// <summary>
        /// 获取指定Layer在Map中结尾图层的Index
        /// </summary>
        /// <param name="id">指定Layer的id</param>
        /// <returns>指定Layer在Map中结尾图层的Index</returns>
        public int GetSpatialMapLayerLastIndex(string id)
        {
            int spatialLastIndex = 0;

            for (int i = 0; i < _innerList.Count; i++)
            {

                if (_innerList[i].Id == id)
                    return spatialLastIndex + _innerList[i].MapLayerCount;

                if (_innerList[i].GetType() == typeof(GroupLayer))
                {
                    if (((GroupLayer)_innerList[i]).GetMapLayerLastIndex(id, ref spatialLastIndex))
                        return spatialLastIndex;
                }
                else
                {
                    spatialLastIndex += _innerList[i].MapLayerCount;
                }
            }

            return spatialLastIndex;
        }

        /// <summary>
        /// 获取指定Layer在Map中所排的起始图层的Index
        /// </summary>
        /// <param name="id">指定Layer的id</param>
        /// <returns>指定Layer在Map中起始图层的Index</returns>
        public int GetSpatialMapLayerStartIndex(string id)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 根据id获取指定图层
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Layer GetLayer(string id)
        {
            Layer layer;


            for (int i = 0; i < _innerList.Count; i++)
            {
                if (_innerList[i].Id == id) return _innerList[i];



                if (_innerList[i].GetType() == typeof(GroupLayer))
                {
                    if ((layer = ((GroupLayer)_innerList[i]).GetChild(id)) != null) return layer;
                }

            }

            return null;

        }

        #endregion

        #region ILIST接口



        public int IndexOf(Layer item)
        {
            return this._innerList.IndexOf(item);
        }

        public void Insert(int index, Layer item)
        {
            this._innerList.Insert(index, item);

            item.DefaultManager = this.DefaultManager;

            //默认给添加到最后一个下面代码更改位置。。。
            item.Render();

            ////更改位置 插入到想要的位置
            //IMapLayerCollection mapLayerCollection = item.DefaultManager.CurrentMap.Layers;
            //IMapLayer imapLayer = mapLayerCollection[mapLayerCollection.Count - 1];

            //imapLayer.LockDispose();
            //mapLayerCollection.RemoveAt(mapLayerCollection.Count - 1);

            ////item.
            //mapLayerCollection.Insert(index, imapLayer);
            //imapLayer.UnlockDispose();
        }

        public void RemoveAt(int index)
        {
            this._innerList.RemoveAt(index);
        }

        public Layer this[int index]
        {
            get
            {
                return this._innerList[index];
            }
            set
            {
                this._innerList[index] = value;
            }
        }

        public void Add(Layer item)
        {
            this._innerList.Add(item);

            item.DefaultManager = this.DefaultManager;

            item.Render();
        }

        public void Clear()
        {
            this._innerList.Clear();
        }

        public bool Contains(Layer item)
        {
            return this._innerList.Contains(item);
        }

        public void CopyTo(Layer[] array, int arrayIndex)
        {
            this._innerList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this._innerList.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(Layer item)
        {
            

            item.StopRender();
            item.DefaultManager = null;

            return this._innerList.Remove(item);
            
        }

        public IEnumerator<Layer> GetEnumerator()
        {
            return this._innerList.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this._innerList.GetEnumerator();
        }

        #endregion
    }
}
